package cn.teduymdj.jxc.service.impl;
/**
 *
 *
 * @author 刘校权
 */
import cn.teduymdj.jxc.ex.ServiceException;
import cn.teduymdj.jxc.mapper.CustomerMapper;
import cn.teduymdj.jxc.pojo.dto.CustomerAddNewDTO;
import cn.teduymdj.jxc.pojo.dto.CustomerUpdateDTO;
import cn.teduymdj.jxc.pojo.entity.Customer;
import cn.teduymdj.jxc.pojo.vo.CustomerListItemVO;
import cn.teduymdj.jxc.pojo.vo.CustomerStandardVO;
import cn.teduymdj.jxc.service.CustomerService;
import cn.teduymdj.jxc.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public Customer getCustomerById(Long customerId) {
        return null;
    }

    @Override
    public List<CustomerListItemVO> getCustomerList(Integer page, Integer pageSize, String customerName) {
        List<CustomerListItemVO> list = customerMapper.list();
        return list;
    }

    @Override
    public Long getCustomerCount(String customerName) {
        return null;
    }

    @Override
    public void saveCustomer(CustomerAddNewDTO customerAddNewDTO) {
        log.debug("开始处理【添加客户】的业务，参数：{}", customerAddNewDTO);
        // 从参数对象中获取客户名称
        String customerName = customerAddNewDTO.getName();
        // 检查客户名称是否已经被占用（客户表中是否已经存在此名称的数据）
        log.debug("检查客户名称是否已经被占用");
        int count = customerMapper.countByName(customerName);
        if (count > 0) {
            // 是：客户名称已经被占用，添加客户失败，抛出异常
            String message = "添加客户失败，客户名称已经被占用！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 否：客户名称没有被占用，则向客户表中插入数据
        log.debug("即将插入客户数据：{}", customerAddNewDTO);
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerAddNewDTO,customer);
        int rows = customerMapper.insert(customer);
        if (rows != 1) {
            String message = "添加客户失败，服务器忙，请稍后再尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("插入客户数据完成");
    }

    @Override
    public void updateCustomer(CustomerUpdateDTO customer) {
        log.debug("开始处理【修改客户详情】的业务， 新数据：{}",  customer);

        // 调用Mapper对象的getDetailsById()方法执行查询
        Long id = customer.getId();
        CustomerStandardVO queryResult = customerMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：此id对应的数据不存在，则抛出异常(ERR_NOT_FOUND)
            String message = "修改客户详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 修改数据
        log.debug("即将修改数据：{}", customer);
        int rows = customerMapper.update(customer);
        if (rows != 1) {
            String message = "修改客户详情失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public void deleteCustomer(Long id) {
        log.debug("开始处理【根据id删除客户】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()执行查询
        CustomerStandardVO queryResult = customerMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：无此id对应的数据，将不允许执行删除操作，则抛出异常
            String message = "删除客户失败，尝试访问的数据不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }


        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        int rows = customerMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除客户失败，服务器忙，请稍后再尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public List<String> getComboboxList() {
        List<String> list = customerMapper.comboboxList();
        return list;
    }
}
